package com.scoressystem.controller;


import com.scoressystem.pojo.Student;
import com.scoressystem.pojo.Teacher;
import com.scoressystem.service.StudentService;
import com.scoressystem.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    StudentService studentService;

    @RequestMapping("/getAllStudent")
    public Results<List<Student>> studentPage(@RequestParam(value = "studentName", defaultValue = "") String studentName,
                                              @RequestParam(value = "page", defaultValue = "1") int pageIndex,
                                              @RequestParam(value = "limit", defaultValue = "5") int pageSize) {

        Map<String, Object> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("pageStart", (pageIndex - 1) * pageSize);
        map.put("pageSize", pageSize);

        List<Student> students = studentService.selectAllStudentsByPage(map);
        long total = studentService.selectAllStudentsCount(map);
        return new Results<>("0", "查询成功", students, total);

    }

    @RequestMapping("/updateStudent")
    public Results<String> updateStudent(Student student) {
        int result = studentService.updateStudent(student);
        if (result > 0) {
            return new Results<>("0", "更新成功");
        } else {
            return new Results<>("1", "更新失败");
        }
    }

    @RequestMapping("/deleteStudent")
    public Results<String> deleteStudent(@RequestParam("studentNo") Integer studentNo) {
        int result = studentService.deleteStudentById(studentNo);
        if (result > 0) {
            return new Results<>("0", "删除成功");
        } else {
            return new Results<>("1", "删除失败");
        }
    }

    @RequestMapping("/addStudent")
    public Results<String> addStudent(Student student) {
        Student existingStudent = studentService.getStudentById(student.getStudentNo());
        if (existingStudent != null) {
            return new Results<>("4", "学生已存在");
        }

        int result = studentService.addStudent(student);

        if (result > 0) {
            return new Results<>("0", "添加成功");
        } else {
            return new Results<>("1", "添加失败");
        }
    }

    @PostMapping("/studentSelect")
    public Results<List<Student>> studentSelect() {

        List<Student> students = studentService.getAllStudentsBySname("");
        if (students.size() > 0) {
            return new Results<>("0", "查询成功", students);
        } else {
            return new Results<>("1", "查询失败");
        }

    }

    @RequestMapping("/login")
    public Results login(Integer userName, String password,
                         String userCode, HttpSession session) {

        Object sysCodeObj = session.getAttribute("systemCode");
        if (sysCodeObj == null) {
            return new Results<>("0", "验证码已过期，请刷新重试");
        }
        String sysCode = sysCodeObj.toString();
        if (!sysCode.equalsIgnoreCase(userCode)) {
            return new Results<>("0", "验证码错误");
        }
        Integer userNo = (Integer) userName;
        Student student = studentService.getStudentByUser(userNo, password);
        if (student == null){
            return new Results<>("2", "用户名或密码错误");
        } else {
            session.setAttribute("nowusername", student.getStudentName());
            session.setAttribute("studentNo", student.getStudentNo());
            return new Results<>("1", "登录成功", student,0);
        }
    }

    @PostMapping("/updatePwd")
    public Results updatePwd(HttpSession session, String pwd, String pwd1, String pwd2) {
        Integer studentNo = (Integer)session.getAttribute("studentNo");
        Student studentByUser = studentService.getStudentByUser(studentNo, pwd);
        if (studentByUser == null) {
            return new Results<>("1", "原密码错误");
        }
        if (!pwd1.equals(pwd2)) {
            return new Results<>("2", "两次新密码输入不一致");
        }
        if (pwd.equals(pwd1)){
            return new Results<>("3", "新密码不能与原密码相同");
        }
        int i = studentService.updateStudentPass(studentNo, pwd1);
        if (i > 0) {
            session.invalidate();
            return new Results<>("0", "密码修改成功，请重新登录");
        } else {
            return new Results<>("5", "密码修改失败，请稍后重试");
        }
    }

}
